# Maintainer: Sergej Pupykin <pupykin.s+arch@gmail.com>
# Contributor: Sebastian Faltoni <sebastian.faltoni@gmail.com>

pkgname=dbmail
pkgver=3.4.0
pkgrel=1
pkgdesc="Fast and scalable sql based mail services"
arch=('loong64' 'x86_64')
url="https://github.com/dbmail/dbmail"
license=('GPL-2.0-or-later')
depends=(
  'glib2'
  'glibc'
  'gmime3'
  'libevent'
  'libzdb'
  'libxcrypt'
  'mhash'
  'openssl'
  'systemd-libs'
)
makedepends=(
  'asciidoc'
  'docbook-xsl'
  'docbook-xml'
  'git'
  'libldap'
  'libsieve'
  'mariadb-libs'
  'postgresql-libs'
  'sqlite'
  'systemd'
  'xmlto'
)
optdepends=(
  'libldap: for LDAP authentication'
  'libsieve: for dbmail-sieve'
)
source=("$pkgname-$pkgver.tar.gz::https://github.com/dbmail/dbmail/archive/refs/tags/v$pkgver.tar.gz"
  https://raw.githubusercontent.com/dbmail/dbmail/main/man/dbmail-sieved.txt
  noop-fetch-response-fix2.patch)
sha256sums=('bcab790cebf3c46c53f05b51bb8fcf7724c8dadd4ee313aa33bd2ae6c2107fe2'
            '0d8d5d27864071823d69cf3ae731a522043d4ce058baf9c00e7c02a1ae98ad64'
            '8d775abeb55f60969641a61b895df22577255f212737f4db01b138dbb40d2064')

prepare() {
  cd $pkgname-$pkgver
  patch -p1 < ../noop-fetch-response-fix2.patch
  cp ../dbmail-sieved.txt ./man/
  sed -i \
    -e 's|@runstatedir@|/run|g' \
    -e 's|dbmail:dbmail|nobody:nobody|g' \
    systemd/*.service.in
  sed -i \
    -e 's|dbmail dbmail|nobody nobody|g' \
    systemd/*.tmpfiles.in

  autoreconf -fiv
}

build() {
  local configure_options=(
    --enable-manpages
    --enable-systemd
    --prefix=/usr
    --runstatedir=/run
    --sbindir=/usr/bin
    --sysconfdir=/etc
    --with-ldap
    --with-sieve
  )

  cd $pkgname-$pkgver
  ./configure "${configure_options[@]}"
  # prevent excessive overlinking due to libtool
  sed -i -e 's/ -shared / -Wl,-O1,--as-needed\0/g' libtool
  make
}

package() {
  cd $pkgname-$pkgver

  install -dm0755 "$pkgdir"/usr/lib/systemd/system
  install -dm0755 "$pkgdir"/usr/lib/tmpfiles.d
  install -dm0755 "$pkgdir"/usr/share/dbmail
  make DESTDIR="$pkgdir" install

  install -Dm644 dbmail.conf "$pkgdir"/etc/dbmail.conf.sample
  cp -r sql/* "$pkgdir"/usr/share/dbmail/
  cp -a contrib "$pkgdir"/usr/share/dbmail/
  cp dbmail.schema "$pkgdir"/usr/share/dbmail/
}
